﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;

namespace TimeTableManagement.Controllers
{
    [Authorize]
    public class RoomController : Controller
    {
        private QLSVContext db = new QLSVContext();

        //
        // GET: /Room/

        public ViewResult Index()
        {
            var tb_room = db.Tb_Room.Include(t => t.Tb_Config).Include(t => t.Tb_Config1);
            return View(tb_room.ToList());
        }

        //
        // GET: /Room/Details/5

        public ViewResult Details(int id)
        {
            Tb_Room tb_room = db.Tb_Room.Find(id);
            return View(tb_room);
        }
        public ViewResult Details2(int id)
        {
            Tb_Room tb_room = db.Tb_Room.Find(id);
            return View(tb_room);
        }
        //
        // GET: /Room/Create

        public ActionResult Create()
        {
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomtype")), "id", "displayword");
            ViewBag.status = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomstatus")), "id", "displayword");
            return View();
        } 

        //
        // POST: /Room/Create

        [HttpPost]
        public ActionResult Create(Tb_Room tb_room)
        {
            if (ModelState.IsValid)
            {
                db.Tb_Room.Add(tb_room);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomtype")), "id", "displayword", tb_room.type);
            ViewBag.status = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomstatus")), "id", "displayword", tb_room.status);
            return View(tb_room);
        }
        
        //
        // GET: /Room/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tb_Room tb_room = db.Tb_Room.Find(id);
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomtype")), "id", "displayword", tb_room.type);
            ViewBag.status = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomstatus")), "id", "displayword", tb_room.status);
            return View(tb_room);
        }

        //
        // POST: /Room/Edit/5

        [HttpPost]
        public ActionResult Edit(Tb_Room tb_room)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_room).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomtype")), "id", "displayword", tb_room.type);
            ViewBag.status = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomstatus")), "id", "displayword", tb_room.status);
            return View(tb_room);
        }

        //
        // GET: /Room/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tb_Room tb_room = db.Tb_Room.Find(id);
            return View(tb_room);
        }

        //
        // POST: /Room/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tb_Room tb_room = db.Tb_Room.Find(id);
            db.Tb_Room.Remove(tb_room);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}